$(document).ready(function(){
	// Клик по кнопкам слайдера
	$('.slider, .slider_active').click(function(){
		changeSlide(this, 'click');
	})

	// Остановить анимацию слайдера при наведении на него
	$('.banner_cont').hover(function(){
		over_slider = true;
	}, function(){
		over_slider = false;
	});

	// Убрать меню при клике по свободному месту
	$('body').click(function(e){
		if (!$(e.target).is('.submenu_container') && !$(e.target).is('.top_menu_item_container') && !$(e.target).is('.underline') && !$(e.target).is('.dotted') && !$(e.target).is('.submenu_container div')) {
			$('.top_menu_item_container .submenu_container').hide('fast');
		}
	});

	slider('start');
})

var over_slider = false;

function slider(action) {
	if (action == 'start') {
		setInterval('changeSlide()', 5000);
	}
}

function changeSlide(obj, action) {
	if (over_slider == true && action == undefined) { return false }

	if (obj == undefined) {
		obj = $('.slider_cont').children('.slider_active').next();
		if (obj.html() == null) { obj = $('.slider_cont').children('div:first'); }
	}

	if ($(obj).attr('class') == 'slider_active') { return false }
	var slideId = $(obj).attr('slide');
	$('.banner_text_cont').children('h1, p, a').hide();
	$('.banner_image:visible').fadeOut(500);
	$('.slider_active').removeClass('slider_active').addClass('slider');
	$('.banner_text_cont').children('h1[slide="'+slideId+'"], p[slide="'+slideId+'"], a[slide="'+slideId+'"]').show();
	$('.banner_image[slide="'+slideId+'"]').fadeIn(500);
	$(obj).removeClass('slider').addClass('slider_active');
}

function show_submenu(obj, _id) {
	var progress = $('<img src="/static/css/images/loading.gif" alt="" class="progress" />');
	//var submenu = $('<div class="submenu"></div>');

	if ($(obj).parent().children('.submenu_container[pid="'+_id+'"]').is('.submenu_container')) {
		if ($(obj).parents('.top_menu_item_container').is('.top_menu_item_container')) {
			$(obj).parents('.submenu_container').find('.submenu_container').hide();
		}

		if ($(obj).parents('.top_menu_item_container').is('.top_menu_item_container')) {
			$(obj).parent().children('.submenu_container[pid="'+_id+'"]').toggle('fast');
		} else {
			$(obj).parent().children('.submenu_container[pid="'+_id+'"]').toggle();
		}
	} else {
		$(obj).append(progress);

		$.post('/ajax/submenu', { id: _id }, function(data){
			$(obj).children('.progress').remove();
			if ($(obj).parents('.top_menu_item_container').is('.top_menu_item_container')) {
				if ($(obj).parents('.top_menu_item_container').is('.top_menu_item_container')) {
					$(obj).parents('.submenu_container').find('.submenu_container').hide();
				}

				$(obj).parent().append(data.data);
				$(obj).parent().children('.submenu_container').show('fast');
			} else {
				$(data.data).insertAfter($(obj).parent().children('span'));
			}
		}, 'json');
	}

	/*$(obj).css('display', 'inline');
	$(obj).parent().children('br').css('display', 'inline');*/
}